package com.caozf.prototype;

import java.io.*;
import java.util.Date;
import java.util.List;

/**
 * 创建时间：2019/9/14 16:01
 * 创建人:曹子方
 * 邮件
 */
public class Email implements  Cloneable, Serializable {
    private Date createtime;
    private String title;
    private String content;
    private String fromUser;
    private String toUser;
    private List<Attachement> attachements;//附件

    public Email() {
    }

    public Email(String title, String content) {
        this.title = title;
        this.content = content;
        this.createtime=new Date();
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getFromUser() {
        return fromUser;
    }

    public void setFromUser(String fromUser) {
        this.fromUser = fromUser;
    }

    public String getToUser() {
        return toUser;
    }

    public void setToUser(String toUser) {
        this.toUser = toUser;
    }

    public List<Attachement> getAttachements() {
        return attachements;
    }

    public void setAttachements(List<Attachement> attachements) {
        this.attachements = attachements;
    }

    @Override
    protected Object clone() {
        try {
            ByteArrayOutputStream bos=new ByteArrayOutputStream();
            ObjectOutputStream oos=new ObjectOutputStream(bos);
            oos.writeObject(this);
            oos.flush();
            oos.close();///最后记得关闭资源，objectOutputStream.close()内部已经将outputStream(ByteArrayOutputStream)对象资源释放了，所以只需要关闭objectOutputStream即可

            ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois=new ObjectInputStream(byteArrayInputStream);
            Email email= (Email)ois.readObject();
            email.createtime=new Date();
            return email;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    protected  Email shallowClone(){
        Email email=new Email();
        email.setTitle(this.getTitle());
        email.setContent(this.getContent());
        email.setAttachements(this.getAttachements());
        email.setCreatetime(new Date());
        email.setFromUser(this.fromUser);
        email.setToUser(this.toUser);
        return email;
    }
}
